<?php

namespace App\Modules\Admin\Services;

use App\Modules\Admin\Repositories\Contracts\CultureVoiceRepository;

class CultureVoiceService
{
    private $cultureVoiceRepository;

    public function __construct(CultureVoiceRepository $cultureVoiceRepository)
    {
        $this->cultureVoiceRepository = $cultureVoiceRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            $result = $this->cultureVoiceRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->cultureVoiceRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            // 转换参数
            $param['content'] = htmlentities($param['content']);
            $cultureVoice = $this->cultureVoiceRepository->store($param);
            return $cultureVoice;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $cultureVoice = $this->cultureVoiceRepository->update($param, $id);
            return $cultureVoice;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $cultureVoice = $this->cultureVoiceRepository->delete($id);

            $result = [];
            if ($cultureVoice) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
